package cn.wolfcode.p2p.hosting.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.hosting.domain.RechargeOnline;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.util.HostingUtil;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.util.Map;


@Service
@Transactional
public class HostingServiceImpl implements IHostingService {

    @Override
    public void createAccount(LoginInfo loginInfo) {
        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "create_activate_member");
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(loginInfo.getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("member_type", "1");
        paramMap.put("client_ip", "192.168.1.2");

        HostingUtil.requestHandler(paramMap);
    }

    @Override
    public void bindPhone(LoginInfo loginInfo) {
        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "binding_verify");
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(loginInfo.getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("verify_type", "MOBILE");
        paramMap.put("verify_entity", HostingUtil.endcodeString(loginInfo.getUsername()));
        paramMap.put("client_ip", "192.168.1.2");

        HostingUtil.requestHandler(paramMap);
    }

    @Override
    public String hostingPersonal(LoginInfo loginInfo) {

        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "show_member_infos_sina");
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(loginInfo.getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("resp_method", "1");
        paramMap.put("return_url", "http://localhost/personal");
        paramMap.put("default_page", "DEFAULT");

        Map<String, String> map = HostingUtil.requestHandler(paramMap);
        return map.get("redirect_url");
    }

    @Override
    public void bindRealInfo(RealAuth realAuth, LoginInfo loginInfo) {

        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "set_real_name");
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(loginInfo.getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("real_name", HostingUtil.endcodeString(realAuth.getRealName()));
        paramMap.put("cert_type", "IC");
        paramMap.put("cert_no", HostingUtil.endcodeString(realAuth.getIdNumber()));
        paramMap.put("client_ip", "192.168.1.2");

        HostingUtil.requestHandler(paramMap);
    }

    @Override
    public String hostingSetPayPassword(LoginInfo loginInfo) {

        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "set_pay_password");
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(loginInfo.getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("client_ip", "192.168.1.2");
        paramMap.put("return_url", "http://localhost/callbackSetPayPassword?uid=" + loginInfo.getId());

        Map<String, String> map = HostingUtil.requestHandler(paramMap);
        return map.get("redirect_url");

    }

    @Override
    public String hostingRecharge(RechargeOnline rechargeOnline) {

        //1.准备参数
        //基本参数
        Map<String, String> paramMap = HostingUtil.getBaseParams();

        //业务参数
        paramMap.put("service", "create_hosting_deposit");
        paramMap.put("out_trade_no", rechargeOnline.getTradeCode());
        paramMap.put("identity_id", HostingUtil.getHostingIdentityId(rechargeOnline.getApplier().getId()));
        paramMap.put("identity_type", "UID");
        paramMap.put("account_type", "BASIC");
        String amount = rechargeOnline.getAmount().setScale(Constants.DISPLAY_SCALE, RoundingMode.HALF_UP).toString();
        paramMap.put("amount", amount);

        paramMap.put("payer_ip", "192.168.1.2");
        paramMap.put("pay_method", "online_bank^" + amount + "^SINAPAY");

        //同步页面跳转地址
        paramMap.put("return_url", "http://localhost/callbackRecharge?tradeCode=" + rechargeOnline.getTradeCode());
        //异步结果通知地址
        paramMap.put("notify_url", "http://localhost/notifyRechargeResult");

        String html = HostingUtil.requestHandlerToString(paramMap, false);
        return html;
    }
}
